var can;
var ctx;

var can2;
var ctx2;

var scoreIMG = new Image();
var levelIMG = new Image();
var nextIMG = new Image();
var nextBG = new Image();
var gridIMG = new Image();
var levelgrids = new Image();
var levelgrid = new Image();
var yourbest = new Image();
var scorecol = new Image();

var score = 0;
var level = 1;
var superb = 0;
var hei;
var No1 = 3455;

var wall;
var fall;
var next;
var stand;
var lamda;

var beta = [
	[0, 0, 0, 0],
	[0, 0, 0, 0],
	[0, 0, 0, 0],
	[0, 0, 0, 0], ];

var l, s, z, o, L, J = new Array();

var NO;
var ran = new Array();

l = [
	[1, 1, 1, 1],
	[0, 0, 0, 0],
	[0, 0, 0, 0],
	[0, 0, 0, 0], ];

s = [
	[0, 1, 1, 0],
	[1, 1, 0, 0],
	[0, 0, 0, 0],
	[0, 0, 0, 0], ];
z = [
	[1, 1, 0, 0],
	[0, 1, 1, 0],
	[0, 0, 0, 0],
	[0, 0, 0, 0], ];

o = [
	[1, 1, 0, 0],
	[1, 1, 0, 0],
	[0, 0, 0, 0],
	[0, 0, 0, 0], ];

L = [
	[1, 0, 0, 0],
	[1, 0, 0, 0],
	[1, 1, 0, 0],
	[0, 0, 0, 0], ];

J = [
	[0, 1, 0, 0],
	[0, 1, 0, 0],
	[1, 1, 0, 0],
	[0, 0, 0, 0], ];
T = [
	[1, 1, 1, 0],
	[0, 1, 0, 0],
	[0, 0, 0, 0],
	[0, 0, 0, 0], ];

ran = [l, s, z, o, L, J, T];

var shiftRan = 8;
var frame = 1;

var wstRow = 0;
var wstCol = 0;

var bgfps = 0;
var bgw = new Array();
var bgwX = 1;
var bgwY = 5;

var SHold = false;

var blingFra = 0;
var fullLine = new Array();
var blingAlpha = 1;
var doBling = false;
var noFullLine = true;
var doFresh = false;

var tchst = 0;
var tchnd = 0;
var tchmove = 0;
var test1;
var test1con;
var test2;
var test2con;
var touchx;
var tOldx;
var touchy;
var tOldy;
var touchUnit=1;
var gapx;
var gapy;

function touchstart(e){
	e.preventDefault();
	tchst = 1;
	tOldx = e.touches[0].clientX;
	tOldy = e.touches[0].clientY;
	touchx = e.touches[0].clientX;
	touchy = e.touches[0].clientY;
}
function touchend(){
	tchnd = 1;
	rotate();
	tchst = 0;
	tchmove = 0;

}
function touchmove(e) {
	e.preventDefault();

	tchmove++;
	touchUnit++;
	if (touchUnit % 5 == 0) {
		touchx = e.touches[0].clientX;
		touchy = e.touches[0].clientY;
		touchUnit = 1;
	}
	move();
}

function move() {
	gapx = touchx - tOldx;
	gapy = touchy - tOldy;
	var gapxABS = Math.abs(gapx)

	if (gapxABS >= Math.abs(gapy)) {
		if (gapxABS > 15) {
			var time = gapxABS / 15;
			if (gapx > 0) {
				for (var i = 0; i < (time - 1); i++) {
					D();
				}
			} else {
				for (var i = 0; i < (time - 1); i++) {
					A();
				}
			}
			tOldx = touchx;
			tOldy = touchy;
		}
	} else {
		if (gapy > 15) {
			var time = gapy / 15;
			for (var i = 0; i < (time - 1); i++) {
				S();
			}
			tOldx = touchx;
			tOldy = touchy;
		}
	}
}

function rotate() {
	if (tchmove == 0) {
		W();
		tchst = 0;
		tchnd = 0;
	}
}

function init() {

	can = document.getElementById('game');
	ctx = can.getContext('2d');

	can2 = document.getElementById('bg');
	ctx2 = can2.getContext('2d');

	test1 = document.getElementById("text1");
	test2 = document.getElementById("text2");

	scoreIMG.src = "images/score.png";
	levelIMG.src = "images/level.png";
	nextIMG.src = "images/next.png";
	gridIMG.src = "images/grid.png";
	nextBG.src = "images/nextframe.png";
	levelgrids.src = "images/levelgrids.png";
	yourbest.src = "images/yourbest.png";
	scorecol.src = "images/scorecol.png";
	levelgrid.src = "images/levelgrid.png";

	var touchable = 'createTouch' in document;

	if (touchable) {
		document.addEventListener('touchstart', touchstart, false);
		document.addEventListener('touchmove', touchmove, false);
		document.addEventListener('touchend', touchend, false);
	} else {
		document.addEventListener('keydown', displace, false);
		document.addEventListener('keyup', SUnhold, false);
	}
	wall = new wallBlock();
	fall = new fallBlock();
	next = new nextBlock();
	stand = new fallBlock();
	lamda = new fallBlock();
	lamda.begin();

	for (var i = 0; i < 4; i++) { //赋值
		fall.arr[i] = new Array(4);
		next.arr[i] = new Array(4);
		stand.arr[i] = new Array(4);
		lamda.arr[i] = new Array(4);
	}

	reInit();
}

function game() {
	init();
	gameloop();
	drawBg(8, 35, 420, 210, 21);
	getBgwArr();
};

function gameloop() {
	test1.innerHTML = "touchy" + touchy;
	test2.innerHTML = "gapx" + gapx;

	hei = 230 + 200 * (1 - score / No1);
	bgfps++;
	//if (bgfps % 50 == 0) {
	//	ctx2.clearRect(0, 0, 320, 480);
	//	drawBg(8, 35, 420, 210, 21);
	//}
	ctx.clearRect(0, 0, 320, 480);
	text();
	drawBlock(wall, 20, 10);
	drawBlock(fall, 4, 4); //画出来
	drawBlock(next, 4, 4);
	
	if (noFullLine == true) {
		for (var j = 0; j < 10; j++) {
			if (wall.arr[0][j] == 1) { //如果gameover restart
				reInit();
			}
		}

		getTopLeft(fall);
		getStartCoor(fall);
		getBottomRight(fall);
		getEndCoor(fall);
		getPointonWall(fall);
		if (frame % shiftRan == 0) {
			checkIfDead(fall);
		}
		blockFalling();

	}else if (doBling == true) {
		linesBling();
		if (blingAlpha == 0) {
			freshWall(fullLine.length, fullLine); //将此行消掉
			doBling = false;
			doFresh = true;
			noFullLine = false;
			blingAlpha = 1;
		}
	}else if (doFresh == true) {
		fullLine = new Array();
		noFullLine = true;
		doFresh = false;
		doBling = false;
	}

	setTimeout(gameloop, 60);
}

function reInit() {

	getRan();
	giveValue(fall.arr, next.arr)

	fall.begin();
	getBottomRight(fall);
	fall.y -= fall.ndRow * 21;
	getRan();

	for (var i = 0; i < 4; i++) { //赋值
		for (var j = 0; j < 4; j++) {
			stand.arr[i][j] = 0;
		}
	}
	//wall size = x7,x215,y37,y456
	for (var i = 0; i < 24; i++) {
		wall.arr[i] = new Array();
		for (var j = 0; j < 10; j++) {
			wall.arr[i][j] = 0;
		}
	}
	for (var i = 0; i < 3; i++) {
		for (var j = 0; j < 10; j++) {
			wall.arr[20 + i][j] = 1;
		}
	}
	score = 0;
	superb = 0;
	level = 1;
}

function giveValue(arr1, arr2) {
	for (var i = 0; i < 4; i++) { //赋值
		for (var j = 0; j < 4; j++) {
			arr1[i][j] = arr2[i][j];
		}
	}
}